from sklearn.datasets import load_iris
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

#加载数据
iris = load_iris()

#转化成dataframe
test_df = pd.DataFrame(iris.data, columns=iris.feature_names)
test_df['category'] = iris.target
print(test_df.info())

#按照花的种类进行分类
grouped = test_df.groupby('category')

#print(list(grouped))


plt.figure(figsize=(12, 5))


plt.subplot(1, 2, 1)

grouped['sepal length (cm)'].get_group(0).plot(kind='kde', alpha=0.5, color='b')
grouped['sepal length (cm)'].get_group(1).plot(kind='kde', alpha=0.5, color='r')
grouped['sepal length (cm)'].get_group(2).plot(kind='kde', alpha=0.5, color='g')
plt.legend(['Setosa', 'Versicolor', 'Virginica'])


plt.subplot(1, 2, 2)
grouped['sepal length (cm)'].get_group(0).plot(bins=50,kind='hist', alpha=0.5, color='b',cumulative=True)
grouped['sepal length (cm)'].get_group(1).plot(bins=50,kind='hist', alpha=0.5, color='r',cumulative=True)
grouped['sepal length (cm)'].get_group(2).plot(bins=50,kind='hist', alpha=0.5, color='g',cumulative=True)
plt.legend(['Setosa', 'Versicolor', 'Virginica'])

plt.savefig('test_hist_pandas.png')



